<?php
/**
 * 辅助函数
 * Created by PhpStorm.
 * User: chenyu
 * Date: 18-7-3
 * Time: 下午8:48
 */
namespace app\common;

use yii\data\Pagination;
use yii\db\ActiveQuery;

class Helpers
{
    /**
     * 批量检查数组的值是否为空
     */
    public static function emptyArr(array $array,array $key)
    {
        foreach ($key as $v){
            empty($array[$v]) && ExceptionHandler::throwCode(1000);
        }
    }

    /**
     * 日志记录
     * @param $msg
     */
    public static function log($msg)
    {
        $file=dirname(__DIR__).'/runtime/logs/exception.log';
        $content=is_array($msg)?var_export($msg,true):$msg;

        file_put_contents($file,$content.PHP_EOL,FILE_APPEND);

    }




    /**
     * 多维数组,取值
     * 用法一,取数组的值:
     * $arr=['a'=>['b'=>['c'=>1]]]
     * val($arr,'a.b.c')
     *
     * 用法二,取数组某列的值
     * $arr=['a'=>['b'=>['c'=>[
     *      ['e'=>1,'f'=>2,'g'=>3],['e'=>4,'f'=>5,'g'=>6]
     * ]]]]
     * val($arr,'a.b.c.*.f')
     * @param $array
     * @param $key
     * @param null $default
     * @return array|mixed|null|string
     */
    public static function getArrayValue($array, $key, $default = null)
    {
        $keyArr = explode('.', $key);
        if (is_array($array) && is_array($keyArr)) {
            while (!is_null($segment = array_shift($keyArr))) {
                if ($segment === '*') {
                    if (!is_null($segment = array_shift($keyArr)) && is_array($array)) {
                        $tmpArr = $default;
                        foreach ($array as $v) {
                            !empty($v[$segment]) && $tmpArr[] = $v[$segment];
                        }
                        $array = $tmpArr;
                    } else {
                        $array = is_null($default) ? $array : $default;
                    }

                    break;
                } else {
                    if(!empty($array[$segment])){
                        $array=$array[$segment];
                    }else{
                        $array=$default;
                        break;
                    }
                }
            }
        } else {
            $array = is_null($default) ? $array : $default;
        }
        return $array;
    }

    /**
     * 约束json输出格式
     */
    public static function webJson($code=0,$msg='success',$data=[])
    {
        return [
            'code'=>$code,
            'msg'=>$msg,
            'data'=>$data
        ];
    }

    /**
     * 分页数量
     * @param $size
     */
    public static function pagination(ActiveQuery $query,$size)
    {
        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $size]);

        $data = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        return ['data' => $data, 'pagination' => $pagination];
    }


    /**
     * 将驼峰命名转换为yii的url
     * @param $name
     * @param string $replace
     * @return string
     */
    public static function parseHumpToUrl($name,$replace='action')
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . '-' . "$2",
            str_replace($replace,'',$name)));
    }

}
